package com.frxuexi.video

import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.CommentBeanItem
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.lib_base.room.FocuseEntity
import com.frxuexi.lib_base.room.LoveEntity
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.video.adapter.VideoAdapter
import com.frxuexi.video.adapter.VideoCommentAdapter
import com.frxuexi.video.databinding.FragmentVideoBinding
import com.frxuexi.video.databinding.PopCommentBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class VideoFragment : BaseFragment<FragmentVideoBinding,VideoViewModel>() {
    val videoAdapter by lazy { VideoAdapter() }
    val videoCommentAdapter by lazy { VideoCommentAdapter() }
    var position=0
    var flag=false//是否点赞
    var facuse=false//是否关注



    override fun initData() {
        vm.videoData.observe(this){
            videoAdapter.apply {
                addData(it)
                notifyDataSetChanged()
                vm.isLove(it.get(0).id)
                vm.isFocuse(it.get(0).userid.toLong())
            }
        }

        vm.addZanData.observe(this){
            ToastUtils.showLong(it.toString())
            Log.i("lr",it.toString())
            val imageView = videoAdapter.getViewByPosition(position, R.id.iv_love) as ImageView
            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_xihuanred)
            val tv = videoAdapter.getViewByPosition(position, R.id.tv_love) as TextView
            tv.setText("1")
        }

        vm.videoError.observe(this){
            ToastUtils.showLong(it)

        }

        vm.isZanData.observe(this){
            flag=true
            val imageView = videoAdapter.getViewByPosition(position, R.id.iv_love) as ImageView
            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_xihuanred)
            val tv = videoAdapter.getViewByPosition(position, R.id.tv_love) as TextView
            tv.setText("1")
        }

        vm.deleteZanData.observe(this){
            if(it>0){
                flag=false
                val imageView = videoAdapter.getViewByPosition(position, R.id.iv_love) as ImageView
                imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_xihuan)
                val tv = videoAdapter.getViewByPosition(position, R.id.tv_love) as TextView
                tv.setText("0")
            }
        }

        vm.commentData.observe(this){
            //刷新列表的评论数
            videoAdapter.data.get(position).commentnum=it.size
            videoAdapter.notifyItemChanged(position)

            LiveDataBus.with("comment",String::class.java).postValue(it.size.toString())

            //加载评论
            videoCommentAdapter.data.clear()
            videoCommentAdapter.addData(it)
            videoCommentAdapter.notifyDataSetChanged()

        }

        vm.addCommentData.observe(this){
            ToastUtils.showLong("评论成功")
            vm.getComments(it.itemid.toString())

        }

        vm.addFocuseData.observe(this){
            facuse=true
            val imageView = videoAdapter.getViewByPosition(position, R.id.iv_add) as ImageView
            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_ok)
        }

        vm.isFocuseData.observe(this){
            facuse=true
            val imageView = videoAdapter.getViewByPosition(position, R.id.iv_add) as ImageView
            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_ok)
        }

        vm.deleteFocuseData.observe(this){
            facuse=false
            val imageView = videoAdapter.getViewByPosition(position, R.id.iv_add) as ImageView
            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_add)
        }
    }

    override fun initView() {

        binding.videoTab.apply {
            addTab(newTab().setText("推荐"),true)
            addTab(newTab().setText("热门"),false)
            addTab(newTab().setText("新闻"),false)
            addTab(newTab().setText("房产"),false)
            addTab(newTab().setText("娱乐"),false)

            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    if(tab?.position==0){
//                        binding.videoLogin.visibility=View.VISIBLE
//                        binding.videoRegister.visibility=View.GONE
                    }else{
//                        binding.videoLogin.visibility=View.GONE
//                        binding.videoRegister.visibility=View.VISIBLE
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })
        }

        val manager = LinearLayoutManager(requireContext())
        binding.videoRv.apply {
            adapter=videoAdapter
            layoutManager=manager
        }

        vm.getVideos(0,10)

        PagerSnapHelper().attachToRecyclerView(binding.videoRv)

        binding.videoRv.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            position = manager.findFirstVisibleItemPosition()
            flag=false
            facuse=false
            val image2 = videoAdapter.getViewByPosition(position, R.id.iv_add) as ImageView
            image2.setImageResource(com.frxuexi.lib_base.R.drawable.ic_add)

            val vid = videoAdapter.data.get(position).id
            vm.getComments(vid.toString())
            val imageView = videoAdapter.getViewByPosition(position, R.id.iv_love) as ImageView
            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_xihuan)
            val tv = videoAdapter.getViewByPosition(position, R.id.tv_love) as TextView
            tv.setText("0")
            val gsy = videoAdapter.getViewByPosition(position, R.id.gsy) as StandardGSYVideoPlayer
            gsy.startPlayLogic()

            vm.isLove(vid)
            vm.isFocuse(videoAdapter.data.get(position).userid.toLong())


        }

        videoAdapter.addChildClickViewIds(R.id.iv_love,R.id.iv_comment,R.id.iv_userIcon)
        videoAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = videoAdapter.data.get(position)
            when(view.id){
                R.id.iv_love->{
                    if(flag){
                        vm.deleteZan(LoveEntity(item.id))
                    }else {
                        vm.addLove(LoveEntity(item.id))
                    }
                }
                R.id.iv_comment->{
                    val window = PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, 1000)
                    val popBinding = PopCommentBinding.inflate(LayoutInflater.from(requireContext()))
                    window.contentView=popBinding.root
                    window.isFocusable=true
                    window.showAtLocation(view,Gravity.BOTTOM,0,0)


                    popBinding.colose.setOnClickListener {
                        window.dismiss()
                    }

                    popBinding.rvComment.apply {
                        this.adapter=videoCommentAdapter
                        layoutManager=LinearLayoutManager(requireContext())
                    }

                    LiveDataBus.with("comment",String::class.java).observe(this){
                        popBinding.etcount.setText("评论数量:${it}")
                    }


                    popBinding.fasong.setOnClickListener {
                        val content = popBinding.et.text.toString()
                        if(content.isNullOrEmpty()){
                            ToastUtils.showLong("评论内容不能为空")
                            return@setOnClickListener
                        }

                        val list = listOf<String>("八维", "aa", "cc")
                        if(list.filter { content.contains(it) }.size>0){
                            ToastUtils.showLong("评论内容不能有敏感词")
                            return@setOnClickListener
                        }
                        val beanItem = CommentBeanItem(
                            userid = 5319,
                            nickname = "lr",
                            itemid = item.id.toString(),
                            createtime = DateTimeUtils.getDateTime(System.currentTimeMillis()),
                            content = content,
                            replyList = emptyList()
                        )
                        vm.addComment(beanItem)
                        popBinding.et.setText("")
                    }
                }
                R.id.iv_userIcon->{
                    if(facuse){
                        vm.deleteFocuse(FocuseEntity(item.userid.toLong()))
                    }else {
                        vm.addFocuse(FocuseEntity(item.userid.toLong()))
                    }
                }
            }
        }
    }

}